package com.dts.gateway.config;

import com.dts.gateway.util.RJLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * （自定义类描述）
 *
 * @author zhoujh
 * @date 2022/06/02 14:35
 */
@Configuration(proxyBeanMethods = false)
public class RJLoadBalancerConfig {
    private final String ZONE_KEY = "gateway.lb.zone";
    private final String ZONE_KEY_V2 = "dts-gateway";
    @Bean
    public ReactorServiceInstanceLoadBalancer rjLoadBalance(LoadBalancerClientFactory loadBalancerClientFactory, Environment environment) {
        /**
         * 随机轮询
         */
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
        // 自定义轮询
        /*        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        String gatewayZone = environment.getProperty(ZONE_KEY_V2);
        return new RJLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),
                name, gatewayZone);*/
    }
}
